//
//  FSLContentView.swift
//  FSLText
//
//  Created by 我演示 on 2019/10/31.
//  Copyright © 2019 ALLSall. All rights reserved.
//

import UIKit


private let kCfontentCellID = "kCfontentCellID"

class FSLContentView: UIView {

    fileprivate var childVcs : [UIViewController]
    fileprivate var parentVc : UIViewController
    
    fileprivate lazy var collectionView : UICollectionView = {
        
        /// 在self的方法中，不能省略
        
        /// 1： 在闭包中，信息不能省略
        
      let layout = UICollectionViewFlowLayout()
        layout.itemSize = self.bounds.size
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = .horizontal
        
        let collectionView = UICollectionView(frame: self.bounds, collectionViewLayout: layout)
      
        collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: kCfontentCellID)
        collectionView.isPagingEnabled = true
        collectionView.showsHorizontalScrollIndicator = false
        ///到最后一个
        collectionView.bounces = false
        collectionView.scrollsToTop = false
        
        collectionView.dataSource = self
        return collectionView
    }()
    
    init (frame: CGRect , childVcs : [UIViewController] , parentVc : UIViewController) {
        self.childVcs = childVcs
        self.parentVc = parentVc
        super.init(frame: frame)
        

        setUpUI()
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    


}


extension FSLContentView {
    
    ///1: 将所有的自控制器添加到父控制器中

    
    fileprivate func setUpUI () {
        /// 将来使用model信息
        
        for childVc in childVcs {
            parentVc.addChild(childVc)
        }
        
        addSubview(collectionView)
        
    }
    
}

extension FSLContentView: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return childVcs.count
    }
    
    /**
     在添加之前，先添加一次
     
     */
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: kCfontentCellID, for: indexPath)
    
        cell.backgroundColor = UIColor.gray
        
        for subView in cell.contentView.subviews {
            // 首先删除其他的
            subView.removeFromSuperview()
        }
        
        let childVc = childVcs[indexPath.item]
        
        childVc.view.frame = cell.contentView.bounds
        
        cell.contentView.addSubview(childVc.view)
        
        return cell
    }
    
    
}
extension FSLContentView: UICollectionViewDelegate {
    
}
